!(function(W,D) {
	W.App = W.App || {
		wxShare: true,
		domain:"",
		debug: false,
        windowWidth : 0
	};
	W.App.getUrl = function(item) {
        var svalue = location.search.match(new RegExp("[\?\&]" + item + "=([^\&]*)(\&?)","i"));
        return svalue ? svalue[1] : svalue;
    }
	;(function() {
		App.ua = navigator.userAgent.toLowerCase();
		App.isAndroid = App.ua.match(/android/i) == "android";
		App.isIOS = App.ua.match(/iphone os/i) == "iphone os";
		App.isIpad = App.ua.match(/ipad/i) == "ipad";
		App.isWM = App.ua.match(/windows ce/i) == "windows ce" || App.ua.match(/windows mobile/i) == "windows mobile";
		var isMidp = App.ua.match(/midp/i) == "midp";
		var isUc7 = App.ua.match(/rv:1.2.3.4/i) == "rv:1.2.3.4";
		var isUc = App.ua.match(/ucweb/i) == "ucweb";
		App.isMobile = App.isAndroid || App.isIOS || App.isIpad || App.isWM || isMidp || isUc7 || isUc;
		App.isWeiXin = App.ua.match(/MicroMessenger/i) == "micromessenger";
		App.isWebKit = App.ua.match(/webkit/i) == "webkit";
		App.isChrome = App.ua.match(/Chrome/i) == "chrome";
		if (App.ua.indexOf('ucbrowser') > -1) {
			var control = navigator.control || {};
			if (control.gesture) {
				control.gesture(false);
			}
		}
	})();	
	function Adaption(obj){
		var s = this;
		this.width = obj.width;
        this.height = obj.height;
        this.type = obj.type;
        this.style = document.createElement('style');
        this.windowWidth = 0;
        this.windowHeight = 0;
        this.aspectRatio = 1;
        this.resizeTime = obj.resizeTime;
        this._resizeId = null;
        console.log('ok')
        this.resizeCallback = function() {
            var  html = document.documentElement,
                cssText = '';
            s.windowWidth = html.clientWidth || window.innerWidth || html.getBoundingClientRect().width;
            s.windowHeight = html.clientHeight || window.innerHeight || html.getBoundingClientRect().height;
            s.aspectRatio = s.windowWidth / s.windowHeight;
            if (s.width && s.height) {
                if (s.aspectRatio > s.width / s.height) {
                    cssText = 'html{font-size:'+s.type?s.windowWidth * 100 / s.width:s.windowHeight * 100 / s.height+'px;}';
                } else {
                    cssText = 'html{font-size:'+s.type?s.windowHeight * 100 / s.height:s.windowWidth * 100 / s.width+'px;}';
                }
            } else if (s.width) {
                cssText = 'html{font-size:'+s.windowWidth * 100 / s.width+'px;}';
            } else {
                cssText = 'html{font-size:'+s.windowHeight * 100 / s.height+'px;}';
            }
            s.style.textContent = cssText;
        };

        this.enable();
        document.head.appendChild(this.style);
	}
	Adaption.prototype.enable = function(){

        this.resizeCallback();
	}
	var  _height = window.innerHeight / (window.innerWidth / 640);
	_height = _height <= 1040 ? 1040 : _height;
	new Adaption({
       height: _height,
       resizeTime: 0,
       type: 0
	});

	if(App.debug || App.getUrl('debug')==1){
		document.write('<script src="js/console.js" type="text/javascript" charset="utf-8"><\/script>');
	}else if( App.getUrl('debug')==2){
		document.write('<script src="js/debug.js" type="text/javascript" charset="utf-8"><\/script>');
	}
   
	if(window.Audio){
		Audio.prototype.autoPlay = function(callback){
			var audio = this;
			audio.play();
			if(audio.paused){
				var ev = function() {
					document.removeEventListener('touchstart', ev, true);
					audio.play();
					callback && callback();
				}
				if (/MicroMessenger/i.test(navigator.userAgent)) {
					if (window.WeixinJSBridge) {
						WeixinJSBridge.invoke('getNetworkType', {}, function(e) {
							audio.play();
							callback && callback();
						});
					} else {
						document.addEventListener("WeixinJSBridgeReady", function() {
							WeixinJSBridge.invoke('getNetworkType', {}, function(e) {
								audio.play();
								callback && callback();
							});
						}, false);
					}
				} else {
					document.addEventListener('touchstart', ev, true);
				}
			}else{
				callback && callback();
			}
		}
	}
	App.Swipe = function() {
	
	}
	App.Swipe.prototype = {
		constructor: App.Swipe,
		init: function(obj) {
			var dom = obj.dom,
				startX, startY, moveX, moveY, endX, endY, limit = obj.limit || 40;
			var end = function(e) {
				var touch = e.changedTouches[0],
					distance, radian, direction;
				endX = touch.pageX;
				endY = touch.pageY;
				distance = Math.sqrt(Math.pow(startX - endX, 2) + Math.pow(startY - endY, 2));
				if (distance < limit) {
					return;
				}
				radian = Math.atan2(endY - startY, endX - startX);
				if (radian >= -0.75 * Math.PI && radian < -0.25 * Math.PI) {
					direction = 'up';
				} else if (radian >= -0.25 * Math.PI && radian < 0.25 * Math.PI) {
					direction = 'right';
				} else if (radian >= 0.25 * Math.PI && radian < 0.75 * Math.PI) {
					direction = 'down';
				} else {
					direction = 'left';
				}
				obj.callback && obj.callback.call(this, direction);
			}
			dom.addEventListener('touchstart', function(e) {
				var touch = e.touches[0];
				startX = touch.pageX;
				startY = touch.pageY;
			});
			dom.addEventListener('touchmove', function(e) {
				e.stopPropagation();
				e.preventDefault();
			},false);
			dom.addEventListener('touchend', function(e) {
				end.call(this, e);
			});
			dom.addEventListener('touchcancel', function(e) {
				end.call(this, e);
			});
		}
	}
	App.Loading = function() {
	
	}
	App.Loading.prototype = {
		constructor: App.Loading,
		init: function(obj) {
			var self = this;
			obj.searchImgs = obj.searchImgs !== undefined ? obj.searchImgs : true;
			self.imgs1 = obj.imgs || [];
			self.imgs2 = [];
			self.debug = obj.debug || false;
			self.imgs3 = [];
			self.imgDoms = [];
			self.bgDoms = [];
			self.imgList = [
				[],
				[],
				[]
			];
			self.audioS = obj.audioS||[];
			self.callback = obj.callback;
			self.enterCallback = obj.enterCallback;
			self.count = 0;
			document.body.offsetHeight;
			if (obj.searchImgs) {
				(function() {
					var imgs, type = obj.searchImgs.type || 'src';
					if (obj.searchImgs.dom) {
						imgs = obj.searchImgs.dom.getElementsByTagName('img');
						console.log(imgs)
					} else {
						imgs = document.images;
					}
					var i = 0,
						l = imgs.length;
					while (i < l) {
						(function(index) {
							var imgDom = imgs[index],
								src = imgDom.getAttribute('data-' + type);
							if (src) {
								imgDom.removeAttribute('data-' + type);
								self.imgDoms.push(imgDom);
								self.imgs2.push(src);
							}
						})(i);
						i++;
					}
				})();
			}
			if (obj.searchBgs) {
				var bgs, type = obj.searchBgs.type || 'bg';
				if (obj.searchBgs.dom) {
					bgs = obj.searchBgs.dom.getElementsByTagName('*');
				} else {
					bgs = document.getElementsByTagName('*');
				}
				var i = 0,
					l = bgs.length;
				while (i < l) {
					(function(index) {
						var bgDom = bgs[index],
							src = bgDom.getAttribute('data-' + type);
						if (src) {
							bgDom.removeAttribute('data-' + type);
							self.bgDoms.push(bgDom);
							self.imgs3.push(src);
						}
					})(i);
					i++;
				}
			}
			self.length = self.imgs1.length + self.imgs2.length + self.imgs3.length+ self.audioS.length;
			if (self.length > 0) {
				//加载第一种用户自定义的图片
				self.loadImgs(self.imgs1, 0);
				//加载第二种页面的图片
				self.loadImgs(self.imgs2, 1);
				//加载第三种背景图片
				self.loadImgs(self.imgs3, 2);
				//加载音乐
				self.loadAudios(self.audioS);
			} else if (obj.callback) {
				obj.callback();
			}
		},
		loadAudios: function(audios) {
			var self = this,
				i = 0,
				l = audios.length;
			for (; i < l; i++) {
				self.loadAudio(audios[i],i);
			}
		},	
		loadAudio : function(audios,i){
			var self = this;
			var music = new Audio();
			music.addEventListener("canplay",function(e){
				self.enter(music);
				this.removeEventListener("canplay",false);
			},false);
			music.addEventListener("error",function(e){
				self.enter(music);
				this.removeEventListener("error",false)
			},false);
			music.src=audios;
			music.load();
		},
		loadImgs: function(imgs, type) {
			var self = this,
				i = 0,
				l = imgs.length;
			for (; i < l; i++) {
				self.loadImage(imgs[i], type, i);
			}
		},
		loadImage: function(imgSrc, type, index) {
			var self = this,
				img;
			switch (type) {
				case 0:
					img = new Image();
					self.imgList[type].push(img);
					img.src = imgSrc;
					break;
				case 1:
					img = self.imgDoms[index];
					self.imgList[type].push(img);
					img.src = imgSrc;
					break;
				case 2:
					img = new Image();
					img.src = imgSrc;
					self.imgList[type].push(img);
					self.bgDoms[index].style.backgroundImage = 'url(' + imgSrc + ')';
					break;
			}
			img.errorCount = 0;
			if (img.complete) {
				self.enter(img);
			} else {
				img.onload = function() {
					img.onload = null;
					self.enter(img);
				}
				img.onerror = function() {
					img.errorCount++;
					if (img.errorCount > 10) {
						img.onerror = null;
						if(self.debug){
							alert('有图片加载失败,' + imgSrc);
						}
						self.enter(img);
					} else {
						img.src = imgSrc;
						if (type == 2) {
							self.bgDoms[index].style.backgroundImage = 'url(' + imgSrc + ')';
						}
					}
				}
			}
		},
		enter: function(img) {
			var self = this;
			self.count++;
			if (self.enterCallback) {
				self.enterCallback(img);
			}
			if (self.count == self.length && self.callback) {
				document.body.offsetHeight;
				self.callback(img);
			}
		}
	}	

	App.AutoPlay = function() {
	
	}
	App.AutoPlay.prototype = {
		constructor: App.AutoPlay,
		init: function(obj) {
			var self = this;
			self.scene = obj.scene;
			self.child = self.scene.children;
			self.time = obj.time || 500;
			self.delayTime = obj.delayTime || 2000;
			self.autoplay = obj.autoplay || false;
			self.className = obj.className || 'hide';
			self.activeName = obj.activeName || false;
			self.loop = typeof obj.loop === 'boolean' ? obj.loop : true;
			self.maxIndex = self.child.length - 1;
			self.curIndex = obj.curIndex || 0;
			self.curIndex = Math.min(self.curIndex, self.maxIndex);
			self.prevIndex = self.curIndex;
			self.type = obj.type || 0;
			self.animateName = obj.animateName || [];
			self.orient = obj.orient || 0;
			self.direction = 0;
			self.canPlay = true;
			self.animate_id = null;
			self.state = 0;
	
			self.start = obj.start || false;
			self.callback = obj.callback || false;
			console.log(self.child)
			var transitionEnd = function(e, target) {
					var that = target,
						child = self.child;
					if (self.prevIndex === null) {
						return;
					}
					if (self.type === 0) {
						if (that !== self.scene) {
							return;
						}
					} else if (that !== child[self.curIndex]) {
						return;
					}
					
					if (e.target !== target) {
						return;
					}
					if (self.type === 1) {
						var c1S = child[self.prevIndex].style,
							c2S = child[self.curIndex].style;
						child[self.prevIndex].classList.add(self.className);
						c1S.webkitTransition = '0s';
						c1S.webkitTransform = 'translate3d(0,0,0)';
						c2S.webkitTransition = '0s';
					}
					end();
				},
				animationEnd = function(e, target) {
					var that = target,
						child = self.child;
					if (self.prevIndex === null || child[self.curIndex] !== target) {
						return;
					}
					if (that !== child[self.curIndex]) {
						return;
					}
					if (e.target !== target) {
						return;
					}
					child[self.prevIndex].classList.add(self.className);
					child[self.curIndex].classList.remove(self.animateName[1]);
					child[self.prevIndex].classList.remove(self.animateName[0]);
					child[self.curIndex].classList.remove(self.animateName[3]);
					child[self.prevIndex].classList.remove(self.animateName[2]);
					end();
				},
				end = function() {
					requestAnimationFrame(function() {
						var child = self.child;
						if (self.activeName) {
							child[self.prevIndex].classList.remove(self.activeName);
							child[self.curIndex].classList.add(self.activeName);
						}
						self.canPlay = true;
						if (self.autoplay && self.state) {
							if (self.direction === 1) {
								self.animate_id = setTimeout(function() {
									self.next();
								}, self.delayTime);
							} else {
								self.animate_id = setTimeout(function() {
									self.prev();
								}, self.delayTime);
							}
						}
						if (self.callback) {
							self.callback();
						}
					});
				};
			if (self.type === 0) {
				if (self.activeName) {
					self.child[self.curIndex].classList.add(self.activeName);
				}
				self.scene.style.cssText += '-webkit-backface-visibility: hidden;-webkit-transform:translate3d(0,0,0);-webkit-transition:-webkit-transform ' + self.time + 'ms;';
				self.scene.offsetWidth;
				self.scene.addEventListener('webkitTransitionEnd', function(e) {
					e.stopPropagation();
					transitionEnd.call(self, e, self.scene);
				});
			} else {
				Array.prototype.forEach.call(self.child, function(v, i) {
					if (self.type === 1) {
						v.style.webkitTransition = '0s';
					}
					v.style.webkitTransform = 'translate3d(0,0,0)';
					v.style.webkitBackfaceVisibility = 'hidden';
					if (i !== self.curIndex) {
						v.classList.add(self.className);
					} else {
						v.classList.remove(self.className);
						if (self.activeName) {
							v.classList.add(self.activeName);
						}
					}
					if (self.type === 1) {
						v.addEventListener('webkitTransitionEnd', function(e) {
							e.stopPropagation();
							transitionEnd.call(self, e, v);
						});
					} else if (self.type === 2) {
						v.addEventListener('webkitAnimationEnd', function(e) {
							e.stopPropagation();
							animationEnd.call(self, e, v);
						});
					}
				});
			}
	
			if (self.autoplay) {
				self.play();
			}
		},
		prev: function() {
			var self = this,
				index = self.curIndex,
				length = self.child.length;
			if (!self.canPlay || (!self.autoplay && !self.loop && index === 0)) {
				return;
			}
			if (self.state) {
				self.pause();
			}
			self.state = 2;
			if (self.start) {
				self.start();
			}
			self.prevIndex = index;
			index--;
			if (index < 0) {
				index += length;
			}
			self.curIndex = index % length;
			self.setIndex();
		},
		next: function() {
			var self = this,
				index = self.curIndex,
				length = self.child.length;
			if (!self.canPlay || (!self.autoplay && !self.loop && index === self.maxIndex)) {
				return;
			}
			if (self.state) {
				self.pause();
			}
			self.state = 1;
			if (self.start) {
				self.start();
			}
			self.prevIndex = index;
			index++;
			self.curIndex = index % length;
			self.setIndex();
		},
		play: function() {
			var self = this;
			self.pause();
			self.direction = 1;
			self.animate_id = setTimeout(function() {
				self.next();
			}, self.delayTime);
		},
		reverse: function() {
			var self = this;
			self.pause();
			self.direction = 2;
			self.animate_id = setTimeout(function() {
				self.prev();
			}, self.delayTime);
		},
		gotoAndPlay: function(index) {
			var self = this;
			self.gotoAndStop(index);
			self.play();
		},
		gotoAndReverse: function(index) {
			var self = this;
			self.gotoAndStop(index);
			self.reverse();
		},
		gotoAndStop: function(index) {
			var self = this;
			if(!self.canPlay){
				return;
			}
			self.pause();
			if (index === self.curIndex) {
				return;
			}
			if (index > self.curIndex) {
				self.state = 1;
			} else {
				self.state = 2;
			}
			if (self.start) {
				self.start();
			}
			if (typeof index === 'number') {
				index = Math.min(index, self.maxIndex);
				self.prevIndex = self.curIndex;
				self.curIndex = index;
			}
			self.setIndex();
		},
		pause: function() {
			var self = this;
			self.state = 0;
			clearTimeout(self.animate_id);
		},
		to: function(index) {
			var self = this,
				child = self.child;
			if (index === self.curIndex || !self.canPlay) {
				return;
			}
			if (self.state) {
				self.pause();
			}
			if (typeof index === 'number') {
				index = Math.min(index, self.maxIndex);
				self.prevIndex = self.curIndex;
				self.curIndex = index;
			}
			if (self.type === 0) {
				var distance = -(self.curIndex / (self.maxIndex + 1) * 100) >> 0,
					next = self.orient ? '0,' + distance + '%,0' : distance + '%,0,0';
				self.scene.style.webkitTransition = '0s';
				self.scene.style.webkitTransform = 'translate3d(' + next + ')';
				self.scene.offsetWidth;
				self.scene.style.webkitTransition = '-webkit-transform ' + self.time + 'ms';
			} else if (self.type === 1) {
				var c1S = child[self.prevIndex].style,
					c2S = child[self.curIndex].style,
					cur = '0,0,0',
					prev = self.orient ? '0,-100%,0' : '-100%,0,0',
					next = self.orient ? '0,100%,0' : '100%,0,0';
				child[self.prevIndex].classList.add(self.className);
				c1S.webkitTransition = '0s';
				c1S.webkitTransform = 'translate3d(0,0,0)';
				c2S.webkitTransition = '0s';
				child[self.curIndex].classList.remove(self.className);
				child[self.curIndex].offsetHeight;
			} else {
				child[self.prevIndex].classList.add(self.className);
				child[self.curIndex].classList.remove(self.className);
			}
			self.canPlay = false;
			requestAnimationFrame(function() {
				if (self.activeName) {
					child[self.prevIndex].classList.remove(self.activeName);
					child[self.curIndex].classList.add(self.activeName);
				}
				self.canPlay = true;
				if (self.autoplay) {
					if (self.direction === 1) {
						self.animate_id = setTimeout(function() {
							self.next();
						}, self.delayTime);
					} else {
						self.animate_id = setTimeout(function() {
							self.prev();
						}, self.delayTime);
					}
				}
				if (self.callback) {
					self.callback();
				}
			});
		},
		setIndex: function() {
			if (!this.canPlay || arguments.length !== 0) {
				return;
			}
	
			var self = this,
				time = self.time,
				child = self.child,
				state = self.state || (self.curIndex > self.prevIndex ? 1 : 2);
			self.canPlay = false;
	
			if (self.type === 0) {
				var distance = -(self.curIndex / (self.maxIndex + 1) * 100) >> 0,
					next = self.orient ? '0,' + distance + '%,0' : distance + '%,0,0';
				self.scene.style.webkitTransform = 'translate3d(' + next + ')';
			} else if (self.type === 1) {
				var c1S = child[self.prevIndex].style,
					c2S = child[self.curIndex].style,
					cur = '0,0,0',
					prev = self.orient ? '0,-100%,0' : '-100%,0,0',
					next = self.orient ? '0,100%,0' : '100%,0,0';
				if (state === 1) {
					c2S.webkitTransform = 'translate3d(' + next + ')';
				} else {
					c2S.webkitTransform = 'translate3d(' + prev + ')';
				}
				child[self.curIndex].classList.remove(self.className);
				child[self.curIndex].offsetHeight;
				requestAnimationFrame(function() {
					if (state === 1) {
						c1S.webkitTransition = '-webkit-transform ' + time + 'ms';
						c1S.webkitTransform = 'translate3d(' + prev + ')';
						c2S.webkitTransition = '-webkit-transform ' + time + 'ms';
						c2S.webkitTransform = 'translate3d(' + cur + ')';
					} else {
						c1S.webkitTransition = '-webkit-transform ' + time + 'ms';
						c1S.webkitTransform = 'translate3d(' + next + ')';
						c2S.webkitTransition = '-webkit-transform ' + time + 'ms';
						c2S.webkitTransform = 'translate3d(' + cur + ')';
					}
				});
			} else {
				child[self.curIndex].classList.remove(self.className);
				if (self.animateName.length === 2 || state === 1) {
					child[self.curIndex].classList.add(self.animateName[1]);
					child[self.prevIndex].classList.add(self.animateName[0]);
				} else {
					child[self.curIndex].classList.add(self.animateName[3]);
					child[self.prevIndex].classList.add(self.animateName[2]);
				}
			}
		}
	}
})(window,document);


